package com.yjzx.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Logger;

/**
 * @author yjzx
 * @date 2023/10/25
 * @description
 */
public class FileUtil {
    private static final Logger logger = Logger.getLogger(FileUtil.class.getName());

    private static final String rootPath = System.getProperty("user.dir");

    /**
     * 添加文件
     *
     * @param mkdirs 是否生成目录
     * @param files  文件
     */
    public static void addFile(boolean mkdirs, File... files) {
        try {
            for (File file : files) {
                if (!file.exists()) {
                    if (mkdirs) {
                        String path = file.getPath();
                        int i = path.lastIndexOf(File.separator) + 1;
                        File dir = new File(path.substring(i));
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }
                    }else {
                        file.createNewFile();
                    }
                }
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 复制文件
     *
     * @param source 原文件
     * @param dest   新文件
     */
    public static void copyFileUsingFileChannels(File source, File dest) {
        logger.info(source.getPath() + "\t\t--copy->\t\t" + dest.getPath());
        try (FileInputStream inputStream = new FileInputStream(source); FileOutputStream outputStream = new FileOutputStream(dest)) {
            outputStream.getChannel().transferFrom(inputStream.getChannel(), 0, inputStream.getChannel().size());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除文件
     *
     * @param delFile 文件全路径
     */
    public static void delFolder(File delFile) {
        if (delFile.isFile()) {
            delFile.delete();
        } else {
            File[] files = delFile.listFiles();
            for (File file : files) {
                delFolder(file);
            }
            delFile.delete();
        }

    }


    /**
     * 获取jar路径
     *
     * @param jarName
     * @param jarPath
     * @return
     */
    public static String toJarPath(String jarName, String jarPath) {

        File file;
        if (jarName != null && !"".equals(jarName.trim())) {
            int index = jarName.lastIndexOf(".jar");
            String substring = jarName.substring(index == -1 ? 0 : index);
            if (".jar".equals(substring)) {
                if (jarPath == null || "".equals(jarPath.trim())) {
                    file = new File(rootPath + File.separator + jarName);
                } else {
                    file = new File(jarPath + File.separator + jarName);
                }
                if (!file.isDirectory()) {
                    return file.getPath();
                }
            }
        }else {
            return null;
        }
        throw new RuntimeException("jar路径不存在，请输入正确路径或将文件放入项目根目录");
    }

    /**
     * 获取一个路径，不存在即生成
     * @param printPath
     * @return
     */
    public static File getPrintPathFile(String printPath) {
        File file;
        if (printPath != null && !"".equals(printPath.trim())) {
            file = new File(printPath);
            if (!file.isAbsolute()) {
                file = new File(rootPath+ File.separator + printPath);
            }
        } else {
            file = new File(rootPath + File.separator);
        }
        addFile(true, file);
        return file;
    }

    /**
     * 更新jar
     *
     * @param finalFile     jar路径名称
     * @param jarName       jar名称
     * @param classPathName class在jar的路径名称
     */
    public static void updateJar(File finalFile, String jarName, String classPathName){
        String command = "jar -uvf " + finalFile.getPath() + File.separator + jarName + " " + classPathName;
        SystemUtil.extracted(finalFile, command);
    }
}
